package cn.echo42.proxy;

import cn.echo42.proxy.factory.ProxyFactory;

import java.lang.reflect.Proxy;

/**
 * @author DaiZhiZhou
 * @file Netty
 * @create 2020-07-30 14:04
 */
public class Client {

    public static void main(String[] args) {
        ProxyFactory<Common> proxyFactory = new ProxyFactory<Common>(
                () -> System.out.println("前置增强"),
                new Target(),
                () -> System.out.println("后置增强")
        );
        Common proxyTarget = proxyFactory.getProxyTarget();

        proxyTarget.function();


        System.out.println(" - - - - - ");
        int i = proxyTarget.functionA();
        System.out.println(i);
    }

    static void proxySample() {
        Common common = new Target();
        // 代理对象
        Common theProxyInstance = (Common) Proxy.newProxyInstance(
            Client.class.getClassLoader(),
            Target.class.getInterfaces(),
            (proxy, method, args1) -> {
                // 通过方法名称具体的处理某一个方法的增强
                if (method.getName().equals("ssss")) {
                    // 处理对应方法名称
                }
                // 同样的我们的参数也可以进行处理
                // Object fallBack = method.invoke(common,args1);
                System.out.println("代理执行！！！");
                // 返回值
                return null;
            }
        );

        theProxyInstance.function();
    }

    public interface Common {
        void function();
        int functionA();
    }

    public static class Target implements Common {
        @Override
        public void function() {
            System.out.println("原方法调用");
        }
        @Override
        public int functionA() {
            return 100;
        }
    }
}



